From 35ac18088644297edf5c686a3ee294258370fe74 Mon Sep 17 00:00:00 2001 From: rndr Date: Sat, 3 Jan 2015 21:33:08 +0500 Subject: [PATCH] Allow using --release flag with --example Closes #831 --- src/bin/run.rs | 11 ++-- src/cargo/core/manifest.rs | 14 +++++ src/cargo/ops/cargo_run.rs | 10 +-- src/cargo/ops/cargo_rustc/mod.rs | 3 +- src/cargo/util/toml.rs | 7 ++- tests/test_cargo_compile.rs | 4 +- tests/test_cargo_profiles.rs | 4 +- tests/test_cargo_run.rs | 104 +++++++++++++++++++++++++++++++ 8 files changed, 140 insertions(+), 17 deletions(-) diff --git a/src/bin/run.rs b/src/bin/run.rs index 473d847bd..7125b68ea 100644 --- a/src/bin/run.rs +++ b/src/bin/run.rs @@ -50,12 +50,11 @@ pub fn execute(options: Options, shell: &mut MultiShell) -> CliResult shell.set_verbose(options.flag_verbose); let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path)); - let env = if options.flag_example.is_some() { - "test" - } else if options.flag_release { - "release" - } else { - "compile" + let env = match (options.flag_release, options.flag_example.is_some()) { + (true, true) => "bench", + (true, false) => "release", + (false, true) => "test", + (false, false) => "compile" }; let mut compile_opts = ops::CompileOptions { diff --git a/src/cargo/core/manifest.rs b/src/cargo/core/manifest.rs index 48b7a7f55..9fabe6bb9 100644 --- a/src/cargo/core/manifest.rs +++ b/src/cargo/core/manifest.rs @@ -178,6 +178,13 @@ impl Profile { } } + pub fn default_example() -> Profile { + Profile { + test: false, + .. Profile::default_test() + } + } + pub fn default_bench() -> Profile { Profile { env: "bench".to_string(), @@ -188,6 +195,13 @@ impl Profile { } } + pub fn default_example_release() -> Profile { + Profile { + test: false, + .. Profile::default_bench() + } + } + pub fn default_release() -> Profile { Profile { env: "release".to_string(), diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 706a74fa7..772828c23 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -39,11 +39,13 @@ pub fn run(manifest_path: &Path, let dst = manifest_path.dir_path().join("target"); let dst = match options.target { Some(target) => dst.join(target), - None => if bin.is_example() { dst.join("examples") } else { dst }, + None => dst, }; - let exe = match bin.get_profile().get_dest() { - Some(s) => dst.join(s).join(bin.get_name()), - None => dst.join(bin.get_name()), + let exe = match (bin.get_profile().get_dest(), bin.is_example()) { + (Some(s), true) => dst.join(s).join("examples").join(bin.get_name()), + (Some(s), false) => dst.join(s).join(bin.get_name()), + (None, true) => dst.join("examples").join(bin.get_name()), + (None, false) => dst.join(bin.get_name()), }; let exe = match exe.path_relative_from(&try!(os::getcwd())) { Some(path) => path, diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index c3d43e2e5..dc7649e0c 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -4,6 +4,7 @@ use std::dynamic_lib::DynamicLibrary; use std::io::USER_RWX; use std::io::fs::{self, PathExtensions}; use std::sync::Arc; +use std::path; use core::{SourceMap, Package, PackageId, PackageSet, Target, Resolve}; use util::{self, CargoResult, human, caused_human}; @@ -783,7 +784,7 @@ fn build_deps_args(mut cmd: CommandPrototype, target: &Target, package: &Package v.push_all(target.get_name().as_bytes()); v.push(b'='); v.push_all(layout.root().as_vec()); - v.push(b'/'); + v.push(path::SEP_BYTE); v.push_all(filename.as_bytes()); cmd = cmd.arg("--extern").arg(v.as_slice()); } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index be863795b..899a875cd 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -814,11 +814,14 @@ fn normalize(libs: &[TomlLibTarget], for ex in examples.iter() { let path = ex.path.clone().unwrap_or_else(|| PathValue::String(default(ex))); - let profile = Profile::default_test().test(false); - let profile = merge(profile, &profiles.test); + let profile = merge(Profile::default_example(), &profiles.test); + let profile_release = merge(Profile::default_example_release(), &profiles.release); dst.push(Target::example_target(ex.name.as_slice(), &path.to_path(), &profile)); + dst.push(Target::example_target(ex.name.as_slice(), + &path.to_path(), + &profile_release)); } } diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 4c333dcc3..2bbf419c7 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -873,9 +873,9 @@ test!(verbose_release_build_deps { --emit=dep-info,link \ -L dependency={dir}{sep}target{sep}release \ -L dependency={dir}{sep}target{sep}release{sep}deps \ - --extern foo={dir}{sep}target{sep}release{sep}deps/\ + --extern foo={dir}{sep}target{sep}release{sep}deps{sep}\ {prefix}foo-[..]{suffix} \ - --extern foo={dir}{sep}target{sep}release{sep}deps/libfoo-[..].rlib` + --extern foo={dir}{sep}target{sep}release{sep}deps{sep}libfoo-[..].rlib` ", running = RUNNING, compiling = COMPILING, diff --git a/tests/test_cargo_profiles.rs b/tests/test_cargo_profiles.rs index 5479fafe9..c4ee0edb3 100644 --- a/tests/test_cargo_profiles.rs +++ b/tests/test_cargo_profiles.rs @@ -101,9 +101,9 @@ test!(top_level_overrides_deps { --emit=dep-info,link \ -L dependency={dir}{sep}target{sep}release \ -L dependency={dir}{sep}target{sep}release{sep}deps \ - --extern foo={dir}{sep}target{sep}release{sep}deps/\ + --extern foo={dir}{sep}target{sep}release{sep}deps{sep}\ {prefix}foo-[..]{suffix} \ - --extern foo={dir}{sep}target{sep}release{sep}deps/libfoo-[..].rlib` + --extern foo={dir}{sep}target{sep}release{sep}deps{sep}libfoo-[..].rlib` ", running = RUNNING, compiling = COMPILING, diff --git a/tests/test_cargo_run.rs b/tests/test_cargo_run.rs index 26ab98e20..bc727a9e6 100644 --- a/tests/test_cargo_run.rs +++ b/tests/test_cargo_run.rs @@ -220,6 +220,110 @@ hello main.rs sep = path::SEP).as_slice())); }); +test!(example_with_release_flag { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + + [dependencies.bar] + version = "*" + path = "bar" + "#) + .file("examples/a.rs", r#" + extern crate bar; + + fn main() { + if cfg!(ndebug) { + println!("fast1") + } else { + println!("slow1") + } + bar::baz(); + } + "#) + .file("bar/Cargo.toml", r#" + [project] + name = "bar" + version = "0.0.1" + authors = [] + + [lib] + name = "bar" + "#) + .file("bar/src/bar.rs", r#" + pub fn baz() { + if cfg!(ndebug) { + println!("fast2") + } else { + println!("slow2") + } + } + "#); + + assert_that(p.cargo_process("run").arg("-v").arg("--release").arg("--example").arg("a"), + execs().with_status(0).with_stdout(format!("\ +{compiling} bar v0.0.1 ({url}) +{running} `rustc src{sep}bar.rs --crate-name bar --crate-type lib \ + -C opt-level=3 \ + --cfg ndebug \ + -C metadata=[..] \ + -C extra-filename=[..] \ + --out-dir {dir}{sep}target{sep}release{sep}deps \ + --emit=dep-info,link \ + -L dependency={dir}{sep}target{sep}release{sep}deps \ + -L dependency={dir}{sep}target{sep}release{sep}deps` +{compiling} foo v0.0.1 ({url}) +{running} `rustc {dir}{sep}examples{sep}a.rs --crate-name a --crate-type bin \ + -C opt-level=3 \ + --cfg ndebug \ + --out-dir {dir}{sep}target{sep}release{sep}examples \ + --emit=dep-info,link \ + -L dependency={dir}{sep}target{sep}release \ + -L dependency={dir}{sep}target{sep}release{sep}deps \ + --extern bar={dir}{sep}target{sep}release{sep}deps{sep}libbar-[..].rlib` +{running} `target{sep}release{sep}examples{sep}a` +fast1 +fast2 +", + compiling = COMPILING, + running = RUNNING, + dir = p.root().display(), + url = path2url(p.root()), + sep = path::SEP).as_slice())); + + assert_that(p.process(cargo_dir().join("cargo")).arg("run").arg("-v").arg("--example").arg("a"), + execs().with_status(0).with_stdout(format!("\ +{compiling} bar v0.0.1 ({url}) +{running} `rustc src{sep}bar.rs --crate-name bar --crate-type lib \ + -g \ + -C metadata=[..] \ + -C extra-filename=[..] \ + --out-dir {dir}{sep}target{sep}deps \ + --emit=dep-info,link \ + -L dependency={dir}{sep}target{sep}deps \ + -L dependency={dir}{sep}target{sep}deps` +{compiling} foo v0.0.1 ({url}) +{running} `rustc {dir}{sep}examples{sep}a.rs --crate-name a --crate-type bin \ + -g \ + --out-dir {dir}{sep}target{sep}examples \ + --emit=dep-info,link \ + -L dependency={dir}{sep}target \ + -L dependency={dir}{sep}target{sep}deps \ + --extern bar={dir}{sep}target{sep}deps{sep}libbar-[..].rlib` +{running} `target{sep}examples{sep}a` +slow1 +slow2 +", + compiling = COMPILING, + running = RUNNING, + dir = p.root().display(), + url = path2url(p.root()), + sep = path::SEP).as_slice())); +}); + test!(run_dylib_dep { let p = project("foo") .file("Cargo.toml", r#" -- 2.30.2